/**
 * Copyright (c) 2018, Alex. All rights reserved.
 */
package com.oschina.bottle.convert.impl;

import com.oschina.bottle.convert.Converter;
import org.springframework.beans.BeanUtils;

/**
 * 默认bean转换，采用属性拷贝策略
 *
 * @author <a href="mailto:zhongchao@gegejia.com">zhong</a>
 * @version 1.0 2018/1/20
 * @since 1.0
 */
public class DefaultBeanConverter<S, T> implements Converter<S, T> {

    @Override
    public T convert(S source, Class<T> targetClass) {
        if(source == null)
        {
            return null;
        }

        T target = BeanUtils.instantiateClass(targetClass);
        copy(source, target);
        return target;
    }

    /**
     * 属性拷贝方法
     *
     * @param source    源对象
     * @param target    目标对象
     */
    protected void copy(S source, T target) {
        BeanUtils.copyProperties(source, target);
    }
}
